<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use app\service\GoodsService;
use app\service\StoreLossService;
use app\service\StoreService;
use app\service\UserService;
use think\Request;

class StoreLossController extends BaseController
{
    /**
     * 获取损耗列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', env('pager.default.size'));
      $base = $request->param('base', []);
      $type = $request->param('type', '');
      $conditions = [];
      if($base){
        $conditions[] = ['base', $base, 'IN'];
      }
      $order = ['apply_date' => 'desc', 'id' => 'desc'];
      $list = StoreLossService::getInstance()->getDataByCondition($conditions, $order);
      $baseList = UserService::getInstance()->storeAll();
      $baseList = array_column($baseList, 'basename', 'base');
      foreach ($list as &$value) {
        $value['basename'] = isset($baseList[$value['base']]) ? $baseList[$value['base']] : '';
      }
      // 分页
      $count = count($list); //总条数
      $start=intval(($page - 1) * $pageSize); //偏移量，当前页-1乘以每页显示条数
      $pager = [
        'totalCount' => $count,
        'pageSize' => intval($pageSize),
        'currPage' => intval($page),
        'totalPage' => intval($count/$pageSize)+1
      ];
      $data = array_slice($list, $start, intval($pageSize));
      return $this->send200ResponseWithPager($data,$pager);
    }

    /**
     * 批量新增损耗
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $detail = $request->param('data', []);
      $sure = $request->param('sure', 0);
      $apply_date = $request->param('apply_date', '');
      $base = $request->param('base', 0);
      if (!$detail) {
        return $this->send400Response('缺少data参数');
      }
      if (!$apply_date) {
        return $this->send400Response('缺少apply_date参数');
      }
      if (!$base) {
        return $this->send400Response('缺少base参数');
      }
      $apply_date = date('Y-m-d', strtotime($apply_date));
      $data = [
        'detail' => $detail,
        'apply_date' => $apply_date,
        'base' => $base
      ];
      $result = StoreLossService::getInstance()->createMultiLoss($data, $sure);
      if (!$result['status']) {
        return $this->send400Response($result['msg']);
      }
      return $this->send200Response('ok');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      // 获取损耗单
      $data = StoreLossService::getInstance()->getDataByCondition([['id', $id, '=']]);
      $data = isset($data[0]) ? $data[0] : [];
      if ($data) {
        $conditions[] = ['sl.loss_id', $id, '='];
        // 获取损耗详情
        $detail = StoreLossService::getInstance()->getLossJoinStore($conditions);
        // 获取当前门店库存
        $stores = StoreService::getInstance()->getDataByCondition([['base', $data['base'], '=']]);
        $stores = array_column($stores, null, 'id');
        // 循环详情 若未保存 则入库前总成本 入库前数量 入库前成本为当前成本
        if ($data['status'] === 2) {
          foreach ($detail as &$value) {
            $value['rest_count'] = isset($stores[$value['store_id']]) ? strval($value['count_before']) - strval($value['count']) : $value['count_before'];
            $value['count_before'] = isset($stores[$value['store_id']]) ? $stores[$value['store_id']]['count'] : 0;
            $value['total_amount_before'] = isset($stores[$value['store_id']]) ? $stores[$value['store_id']]['total_amount'] : 0;
            $value['price_before'] = isset($stores[$value['store_id']]) ? $stores[$value['store_id']]['price'] : 0;
          }
        }
        $data['detail'] = $detail;
      }
      return $this->send200Response($data);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
      $data = $request->param('data', []);
      if (!$data) {
        return $this->send400Response('更新数据不能为空');
      }
      $sure = $request->param('sure', 0);
      $result = StoreLossService::getInstance()->updateMultiLoss($id, $data, $sure);
      if (!$result['status']) {
        return $this->send400Response($result['msg']);
      }
      return $this->send200Response('ok');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 根据编号获取损耗详情
     * @return \think\Response
     */
    public function getByNumber(Request $request){

    }
}
